//
//  PopUtil.swift
//  swiftDemo
//
//  Created by zw on 2017/7/12.
//  Copyright © 2017年 zev. All rights reserved.
//

import UIKit
import pop
class PopUtil: NSObject {
    
    
    /// popBasicAnimation 基础动画
    ///
    /// - Parameters:
    ///   - type: 动画类型 参考POPAnimatableProperty
    ///   - key: key
    ///   - duration: 时间
    ///   - fromValue: from
    ///   - toValue: to
    ///   - target: 作用目标view，layer等
    ///   - beginTime: 开始时间，默认为系统当前时间
    ///   - repeatCount: 重复次数，0为无限重复，默认为1
    ///   - timingFunction: 速度控制函数，默认为kCAMediaTimingFunctionDefault
    /// - Returns: anim,可得到当前执行的动画对象
    @discardableResult
    static func popBasicAnimation(type:String,key:String,duration:TimeInterval,fromValue:Any?,toValue:Any,target:NSObject,beginTime:TimeInterval=CACurrentMediaTime(),repeatCount:Int=1,timingFunction:CAMediaTimingFunction=CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.linear)) -> POPAnimation {
        var anim = target.pop_animation(forKey: key) as? POPBasicAnimation
        if (anim != nil) {
            anim!.duration = duration
            anim!.fromValue = fromValue
            anim!.toValue = toValue
            anim!.beginTime = beginTime
            anim!.timingFunction = timingFunction
            if repeatCount==0 {
                anim!.repeatForever = true
            }else{
                anim!.repeatForever = false
                anim!.repeatCount = repeatCount
            }
        }else{
            anim = POPBasicAnimation.init(propertyNamed: type)
            anim!.duration = duration
            anim!.fromValue = fromValue
            anim!.toValue = toValue
            anim!.beginTime = beginTime
            anim!.timingFunction = timingFunction
            if repeatCount==0 {
                anim!.repeatForever = true
            }else{
                anim!.repeatForever = false
                anim!.repeatCount = repeatCount
            }
        }
        target.pop_add(anim, forKey: key)
        return anim!;
    }
    
    
    /// popSpringAnimation 弹簧动画
    ///
    /// - Parameters:
    ///   - type: 动画类型 参考POPAnimatableProperty
    ///   - key: key
    ///   - speed: 速度 越大则动画结束越快
    ///   - bounciness: 弹力 越大则震动幅度越大
    ///   - friction: 摩擦
    ///   - tension: 拉力
    ///   - mass: 质量
    ///   - fromValue: from
    ///   - toValue: to
    ///   - target: 作用目标view，layer等
    ///   - beginTime: 开始时间，默认为系统当前时间
    ///   - repeatCount: 重复次数，0为无限重复，默认为1
    ///   - timingFunction: 速度控制函数，默认为kCAMediaTimingFunctionDefault
    /// - Returns: anim,可得到当前执行的动画对象
    @discardableResult
    static func popSpringAnimation(type:String,key:String,speed:CGFloat,bounciness:CGFloat,friction:CGFloat=0.0,tension:CGFloat=0.0,mass:CGFloat=0.0,fromValue:Any?,toValue:Any,target:NSObject,beginTime:TimeInterval=CACurrentMediaTime(),repeatCount:Int=1) -> POPAnimation {
        var anim = target.pop_animation(forKey: key) as? POPSpringAnimation
        if (anim != nil) {
            anim!.springSpeed = speed;
            anim!.springBounciness = bounciness;
            anim!.dynamicsFriction = friction;
            anim!.dynamicsTension = tension
            anim!.dynamicsMass = mass
            anim!.fromValue = fromValue
            anim!.toValue = toValue
            anim!.beginTime = beginTime
            if repeatCount==0 {
                anim!.repeatForever = true
            }else{
                anim!.repeatForever = false
                anim!.repeatCount = repeatCount
            }
        }else{
            anim = POPSpringAnimation.init(propertyNamed: type)
            anim!.springSpeed = speed;
            anim!.springBounciness = bounciness;
            anim!.dynamicsFriction = friction;
            anim!.dynamicsTension = tension
            anim!.dynamicsMass = mass
            anim!.fromValue = fromValue
            anim!.toValue = toValue
            anim!.beginTime = beginTime
            if repeatCount==0 {
                anim!.repeatForever = true
            }else{
                anim!.repeatForever = false
                anim!.repeatCount = repeatCount
            }
        }
        target.pop_add(anim, forKey: key)
        return anim!;
    }
    
    
    /// popDecayAnimation 衰减动画
    ///
    /// - Parameters:
    ///   - type: 动画类型 参考POPAnimatableProperty
    ///   - key: key
    ///   - velocity: 速率 （和fromValue是相同类型）
    ///   - deceleration: 衰减系数(越小则衰减得越快) 默认0.998
    ///   - fromValue: from (toValue是动态计算的，传入也无效)
    ///   - target: 作用目标view，layer等
    ///   - beginTime: 开始时间，默认为系统当前时间
    ///   - repeatCount: 重复次数，0为无限重复，默认为1
    /// - Returns: anim,可得到当前执行的动画对象
    @discardableResult
    static func popDecayAnimation(type:String,key:String,velocity:Any,deceleration:CGFloat=0.998,fromValue:Any?,target:NSObject,beginTime:TimeInterval=CACurrentMediaTime(),repeatCount:Int=1) -> POPAnimation {
        var anim = target.pop_animation(forKey: key) as? POPDecayAnimation
        if (anim != nil) {
            anim!.velocity = velocity
            anim!.deceleration = deceleration
            anim!.fromValue = fromValue
            anim!.beginTime = beginTime
            if repeatCount==0 {
                anim!.repeatForever = true
            }else{
                anim!.repeatForever = false
                anim!.repeatCount = repeatCount
            }
        }else{
            anim = POPDecayAnimation.init(propertyNamed: type)
            anim!.velocity = velocity
            anim!.deceleration = deceleration
            anim!.fromValue = fromValue
            anim!.beginTime = beginTime
            if repeatCount==0 {
                anim!.repeatForever = true
            }else{
                anim!.repeatForever = false
                anim!.repeatCount = repeatCount
            }
        }
        target.pop_add(anim, forKey: key)
        return anim!;
    }
}
